*Bartek Woda woda@uchicago.edu

global maindir "C:\Users\barte\Dropbox\Migration Data\Project 1 - BLAs"
global codingfile "09_27_21"
global assgnfile "09_22_21"
global blafile "09_21_21"

cap pr drop asterisk_p asterisk_b

pr def asterisk_p
	args p
	if `p'<=0.1 & `p'>0.05 global pval `"`=string(`p',"%7.3f")'*"'
	else if `p'<=0.05 & `p'>0.01 global pval `"`=string(`p',"%7.3f")'**"'
	else if `p'<=0.01 global pval `"`=string(`p',"%7.3f")'***"'
	else global pval `"`=string(`p',"%7.3f")'"'
end

pr def asterisk_b
	args p b
	if `p'<=0.1 & `p'>0.05{
		global bcoef `"`=string(`b',"%12.3g")'*"'
		global starsonly "*"
	}
	else if `p'<=0.05 & `p'>0.01{
		global bcoef `"`=string(`b',"%12.3g")'**"'
		global starsonly "**"
	}
	else if `p'<=0.01{
		global bcoef `"`=string(`b',"%12.3g")'***"'
		global starsonly "***"
	}
	else{
		global bcoef `"`=string(`b',"%12.3g")'"'
		global starsonly "."
	}
end

********************************************************************************
**#*************************** 0 - BLA coding **********************************
********************************************************************************

use "$maindir/Raw data files/treaties lists/BLA coding/bla_coding_$codingfile", clear

*Among the double coded treaties, keeping only the first one recorded
gen time_complete = clock(RecordedDate, "MDYhm")
format time_complete %tC

gen ID = regexs(0) if regexm(bla_ID, "[0-9]+")
destring ID, replace

*Verifying with assignments
merge m:1 ID using "$maindir/Raw data files/treaties lists/BLA coding/coder_assignments_ALL_$assgnfile"
drop if _merge==2
drop _merge

sort ID time_complete
by ID: gen response_num = _n
by ID: gen response_tot = _N

/*some are marked as not double coded but have multiple responses
browse ID coder_name coder1 time_complete coded_countryA coded_countryB coded_year if response_tot==2 & double_coded==0 
*/

replace ID=79 if ID==828 & response_num==2 //the ID is erroneously recorded
replace ID=1064 if ID==974 & response_num==2 //the ID is erroneously recorded

replace double_coded = 1 if ID==695 //Adrian coded 695 instead of 696
replace coder2 = "Adrian" if ID==695

replace coded_year = 1962 if ID==579 //the agreement is correct but the year is not

replace ID=1155 if ID==1153 & coder_name=="B. Posdal" //Brad actually coded 1155

replace coded_year = 1961 if ID==31 //the agreement is correct but the year is not

replace ID = 93 if ID==197 & coder_name=="M. Maxson" //the ID is erroneously recorded
replace ID = 343 if ID==986 & coder_name=="M. Maxson" //the ID is erroneously recorded
replace ID = 1135 if ID==1035 & coder_name=="M. Maxson" //the ID is erroneously recorded

replace coded_year = 1952 if ID==253 //wrong year in the file name

replace coded_countryB = "Czechoslovakia" if ID==478
replace coded_countryB = "West Germany" if ID==442
replace coded_countryA = "West Germany" if ID==522
replace coded_countryB = "West Germany" if ID==551
replace coded_countryB = "South Korea" if ID==884


/*
preserve

keep if response_tot==2 & double_coded==1

keep coder_name coder1 coder2 response_num coded_country* coded_year agreement_title ID

reshape wide coder_name coder1 coder2 coded_countryA coded_countryB coded_year agreement_title, i(ID) j(response_num)

restore

*/

*Standardizing names
foreach l in A B{
 
	replace coded_country`l'="United States of America" if coded_country`l'=="United States"
	replace coded_country`l'="West Germany" if coded_country`l'=="West Germany (German Federal Republic)"
	replace coded_country`l'="East Germany" if coded_country`l'=="East Germany (German Democratic Republic)"
	replace coded_country`l'="United Arab Emirates" if coded_country`l'=="United Arab Emirates (UAE)"
}

drop response_num response_tot

sort ID time_complete
by ID: gen response_num = _n
by ID: gen response_tot = _N

drop if response_num==2

save "$maindir/Raw data files/treaties lists/BLA coding/bla_coding_first_$codingfile", replace

********************************************************************************
**#************************* 1 - BLA coding graphs *****************************
********************************************************************************

use "$maindir/Raw data files/treaties lists/global_BLAs_$blafile", clear

merge 1:1 ID using "$maindir/Raw data files/treaties lists/BLA coding/bla_coding_first_$codingfile"

keep if _merge==3

/*Confirming the data are correct

gen data_correct = 1 if (countryA==coded_countryA & countryB==coded_countryB | countryA==coded_countryB & countryB==coded_countryA) & year==coded_year
replace data_correct = 0 if data_correct==. & _merge==3

browse ID countryA countryB year coded_countryA coded_countryB coded_year agreement_title ///
	if data_correct==0

*/

gen governance_title  = 0, b(intl_instr)
lab var governance_title "{bf:Governance of Labor Migration}"

gen protection_title = 0, b(migrant_info)
lab var protection_title "{bf:Protection and Empowerment of Migrant Workers}"
gen protection_space = 0, b(protection_title)
lab var protection_space " "

gen dev_title = 0, b(mig_human_cap)
lab var dev_title "{bf:Migration and Development}"
gen dev_space = 0, b(dev_title)
lab var dev_space " "

vl create barvars = (governance_title-recruit_auth labor_union_orig-perm_resid)

vl create barvars1 = (intl_instr-recruit_auth labor_union_orig-ngos)
vl create barvars2 = (migrant_info-disputes_emp_mig)
vl create barvars3 = (mig_human_cap-perm_resid)

*1) Histogram by year

#delimit;

hist year if year>=1945, discrete frequency color(gs12) lc(black) ylab(#8, angle(0))
	xlab(1945(5)2020, angle(45) labsize(*0.6)) graphregion(color(white))
	name(coded_hist, replace) yt("") xt("");

#delimit cr

graph export "$maindir/graphs and maps/coded_hist.pdf", replace
graph export "$maindir/graphs and maps/coded_hist.emf", replace

*2) graph of prevalence of provisions

global barvarlabs ""
local i = 1
foreach var in $barvars {
    
	global barvarlabs `"$barvarlabs `i' "`:var lab `var''""'
	local `i++'
	
}

*a) all provisions
#delimit;

graph hbar $barvars , ascat yvar(relabel($barvarlabs ) label(labsize(*0.5))) 
	bar(1, color(gs11)) graphregion(color(white)) ylab(0(0.1)0.7) name(provisions_bargraph_all, replace);

#delimit cr

graph export "$maindir/graphs and maps/provisions_bargraph_all.pdf", replace
graph export "$maindir/graphs and maps/provisions_bargraph_all.emf", replace

*3) provision by year

foreach var in $barvars{
 
	local lab`var' "`:var lab `var''"
	
}

collapse (sum) $barvars (count) ID, by(year)

drop if year<1945

foreach var in $barvars{
 
	lab var `var' "`lab`var''"
	
}

sort year
gen sum_tot = sum(ID)
foreach var in $barvars{
 
	gen sum_`var' = sum(`var')
	gen cshare_`var' = sum_`var'/sum_tot
	
}

order cshare*, last

vl create cshbarvars1 = (cshare_intl_instr-cshare_ngos)
vl create cshbarvars2 = (cshare_migrant_info-cshare_disputes_emp_mig)
vl create cshbarvars3 = (cshare_mig_human_cap-cshare_perm_resid)


*a) just lines

foreach var in $barvars{

	#delimit ;
	
	twoway line cshare_`var' year, lc(black) xlab(#16, angle(45) labsize(*0.55)) ylab(0(0.2)1, angle(0))
		title("`:var lab `var''", size(*0.4)) graphregion(color(white)) name(`var', replace)
		yt("") xt("");
	
	#delimit cr
	
}

graph combine $barvars1, graphregion(color(white)) name(governance_lines, replace)
graph export "$maindir/graphs and maps/governance_lines.pdf", replace
graph export "$maindir/graphs and maps/governance_lines.emf", replace
graph combine $barvars2, graphregion(color(white)) name(protection_lines, replace)
graph export "$maindir/graphs and maps/protection_lines.pdf", replace
graph export "$maindir/graphs and maps/protection_lines.emf", replace
graph combine $barvars3, graphregion(color(white)) name(development_lines, replace)
graph export "$maindir/graphs and maps/development_lines.pdf", replace
graph export "$maindir/graphs and maps/development_lines.emf", replace

*b) lines + bar

foreach var in $barvars{
	#delimit ;
	
	twoway  (line cshare_`var' year, lc(black) yaxis(1)) (bar `var' year, color(eltblue%30) yaxis(2)), 
		xlab(#16, angle(45) labsize(*0.55)) ylab(0(0.2)1, angle(0) axis(1)) ylab(0(5)20, angle(0) axis(2))
		title("`:var lab `var''", size(*0.4)) graphregion(color(white)) name(cshare_`var', replace)
		yt("", axis(1)) yt("", axis(2)) xt("") legend(off);
	
	#delimit cr
	
}

graph combine $cshbarvars1, graphregion(color(white)) name(governance_blines, replace)
graph export "$maindir/graphs and maps/governance_cshares.pdf", replace
graph export "$maindir/graphs and maps/governance_cshares.emf", replace
graph combine $cshbarvars2, graphregion(color(white)) name(protection_blines, replace)
graph export "$maindir/graphs and maps/protection_cshares.pdf", replace
graph export "$maindir/graphs and maps/protection_cshares.emf", replace
graph combine $cshbarvars3, graphregion(color(white)) name(development_blines, replace)
graph export "$maindir/graphs and maps/development_cshares.pdf", replace
graph export "$maindir/graphs and maps/development_cshares.emf", replace

********************************************************************************
**#************************ 2 - Individual BLA data ****************************
********************************************************************************

use "$maindir/Raw data files/treaties lists/global_BLAs_$blafile", clear

drop if year<1945 | year>2021

*Unique dyad
egen udyad = tag(dyad_id)

*Time periods
gen time_int = ceil((year-1945)/10)

*Language
gen code_language = 1 if doc_language=="English" & copyavailable==1 & google_translate==0
replace code_language = 2 if google_translate==1 & copyavailable==1 & !regexm(doc_language, "Spanish") ///
	& !regexm(doc_language, "Portugese") & !regexm(doc_language, "Arabic")
replace code_language = 3 if copyavailable==1 & regexm(doc_language, "Arabic")
replace code_language = 4 if copyavailable==1 & regexm(doc_language, "Spanish")
replace code_language = 5 if copyavailable==1 & regexm(doc_language, "Portugese")
replace code_language = 6 if copyavailable==1 & missing(code_language)

*1) Number of treaties over time

preserve

	collapse (count) ID (sum) udyad, by(year)
	sort year
	gen treatysum = sum(ID)
	gen dyadsum = sum(udyad)
	
	#delimit;
	
	twoway (bar ID year, yaxis(1) color(navy%50))
		(line treatysum year, lc(black) yaxis(2)) (line dyadsum year, lc(black) lp(dash) yaxis(2))
		, xlab(#15, angle(45) labsize(*0.7)) ylab(#10, labsize(*0.7) angle(0) axis(1))
		ylab(#10, labsize(*0.7) angle(0) axis(2))
		graphregion(color(white)) xt("") yt("") name(bla_sum, replace)
		legend(order( 2 "Cumulative BLAs signed" 3 "Cumulative unique dyads with BLAs" 1 "BLAs signed") rows(2) size(*0.6));
	
	#delimit cr

	graph export "$maindir/graphs and maps/bla_sum.pdf", replace
	graph export "$maindir/graphs and maps/bla_sum.emf", replace

restore

*2) Share of copies and evidence over time

preserve

	collapse (mean) copyavailable evidence, by(time_int)
	
	#delimit ;
	
	graph bar copyavailable evidence, over(time_int, relabel (1 "1946-1955" 2 "1956-1965"
		3 "1966-1975" 4 "1976-1985" 5 "1986-1995" 6 "1996-2005" 7 "2006-2015" 8 "2016-2020") 
		label(labsize(*0.65) angle(0))) ylab(#10, angle(0) labsize(*0.8)) bar(1, c(gs6))
		bar(2, c(gs12)) graphregion(color(white)) legend(order(1 "Copy of BLA" 2 "Evidence of BLA"))
		name(bla_evid_time, replace);
	
	#delimit cr
	
	graph export "$maindir/graphs and maps/bla_evid_time.pdf", replace
	graph export "$maindir/graphs and maps/bla_evid_time.emf", replace

restore

*3) Treaty languages over time

#delimit ;

graph bar (count) copyavailable, over(code_lang) over(time_int, relabel (1 "1946-1955" 2 "1956-1965"
	3 "1966-1975" 4 "1976-1985" 5 "1986-1995" 6 "1996-2005" 7 "2006-2015" 8 "2016-2020") 
	label(labsize(*0.65) angle(0))) stack percent asyvar ylab(#10, angle(0) labsize(*0.8))
	legend(order(1 "English (original or official translation)" 2 "English (Google Translate)" 
	3 "Arabic" 4 "Spanish" 5 "Portugese" 6 "Other") rows(3) size(*0.8))
	bar(1, c(gs3)) bar(2, c(gs5)) bar(3, c(gs7)) bar(4, c(gs9)) bar(5, c(gs11))
	bar(6, c(gs13)) yt("") graphregion(color(white)) name(doc_lang_by_period, replace);

#delimit cr
	
graph export "$maindir/graphs and maps/doc_lang_by_period.pdf", replace
graph export "$maindir/graphs and maps/doc_lang_by_period.emf", replace

********************************************************************************
***************************** 3 - Country data *********************************
********************************************************************************

use "$maindir/nat_BLA_data_1945_2020", clear

tsset code year

*Copy available 
egen copyavailable_tot = rowtotal(copyavailable?)

by code: gen copyavailable_totsum = sum(copyavailable_tot)

gen copyshare_totsum = copyavailable_totsum/bla_totsum

*0) Countries with most copies

preserve

	keep if year==2020
	replace NAMES_STD="Northern Mariana Islands" if NAMES_STD=="3"
	replace NAMES_STD="Montenegro" if NAMES_STD=="341"
	replace NAMES_STD="Kosovo" if NAMES_STD=="347"
	replace NAMES_STD="South Sudan" if NAMES_STD=="626"
	replace NAMES_STD="Hong Kong" if NAMES_STD=="711"
	gsort - bla_totsum + NAMES_STD
	drop if bla_totsum==0
	
	keep NAMES_STD bla_totsum copyshare_totsum
	replace copyshare_totsum = copyshare_totsum*100
	format copyshare_totsum %5.4g
	
	export excel "$maindir/graphs and maps/copies_by_country.xlsx", replace firstrow(variables)

restore

*1) Number of countries with at least 1 BLA - line graph

	*a) All countries
	
	preserve

		collapse (sum) bla_past, by(year)
		
		#delimit ;
		
		twoway (line bla_past year, lc(gs8) lw(*2)), xlab(#15, angle(45) labsize(*0.7)) 
			ylab(#15, labsize(*0.7) angle(0)) graphregion(color(white)) xt("") yt("") 
			name(bla_countries, replace);		
		
		#delimit cr
		
		graph export "$maindir/graphs and maps/bla_countries.pdf", replace
		graph export "$maindir/graphs and maps/bla_countries.emf", replace

	restore

	*b) By region

	preserve

		collapse (sum) bla_past, by(year region)
		
		#delimit ;
		
		twoway (line bla_past year if region=="Africa" , lc(black) lw(*2) )
			(line bla_past year if region=="Americas", lc(black) lw(*0.5))
			(line bla_past year if region=="Asia", lc(black) lp(dash) )
			(line bla_past year if region=="Europe", lc(gs10) lp(dash) )
			(line bla_past year if region=="Oceania", lc(black) lp(dot) lw(*2)), 
			legend(order(1 "Africa" 2 "Americas" 3 "Asia" 4 "Europe" 5 "Oceania") 
			rows(2)) xlab(#15, angle(45) labsize(*0.7)) ylab(#5, labsize(*0.7) angle(0))
			graphregion(color(white)) xt("") yt("") name(bla_countries_region, replace);
		
		#delimit cr
		
		graph export "$maindir/graphs and maps/bla_countries_region.pdf", replace
		graph export "$maindir/graphs and maps/bla_countries_region.emf", replace

	restore

*2) Map of countries with BLAs
	
	preserve
	
	use "$maindir/Raw data files/world map files/oldworlddb_plus", clear
	drop if COWEYEAR<2015
	destring COWCODE, gen(code)
	
	tempfile mapcodes
	save `mapcodes'
	
	restore
	
	preserve
	
	keep if year==2020
	merge 1:1 code using `mapcodes'
	
	#delimit;
	spmap bla_totsum using "$maindir/Raw data files/world map files/oldworldcoord_plus", 
		polygon(d("$maindir/Raw data files/world map files/worldcoord") select(keep if _ID==59))
		id(_ID) clm(custom) clb(0 0.9 1 2 5 10 50 1000000)
		fcolor(white gs13 gs11 gs9 gs7 gs5 gs3) 
		legtitle("Total BLAs") title("") name(tot_blas_map, replace)
		legend(label(2 "0") label(3 "1") label(4 "2") label(5 "3-5") label(6 "6-10") 
		label(7 "11-50") label(8 ">50") pos(9) symy(5) symx(5) textw(10) size(3));
		
	#delimit cr
	
	graph export "$maindir/graphs and maps/tot_blas_map.pdf", replace
	graph export "$maindir/graphs and maps/tot_blas_map.emf", replace
	
	restore
	
*3) Histogram of number of BLAs signed by 2020

	gen bla_tot_cat = 0 if bla_totsum==0
	replace bla_tot_cat = 1 if bla_totsum==1
	replace bla_tot_cat = 2 if bla_totsum==2
	replace bla_tot_cat = 3 if bla_totsum>=3 & bla_totsum<=5
	replace bla_tot_cat = 4 if bla_totsum>=6 & bla_totsum<=10
	replace bla_tot_cat = 5 if bla_totsum>=11 & bla_totsum<=50
	replace bla_tot_cat = 6 if bla_totsum>50
	
	#delimit;
	
	graph bar (percent) code if year==2020, over(bla_tot_cat, relabel(1 "0" 2 "1" 3 "2" 
		4 "3-5" 5 "6-10" 6 "11-50" 7 ">50") label(labsize(*0.8))) ylab(#10, labsize(*0.8) angle(0))
		yt("Percent of countries (2020)") graphregion(color(white)) name(bla_tot_hist, replace)
		bar(1, color(gs7));
	
	#delimit cr
	
	graph export "$maindir/graphs and maps/bla_tot_hist.pdf", replace
	graph export "$maindir/graphs and maps/bla_tot_hist.emf", replace

*4) Probability of a BLA conditional on past bla

	gen l1_bla_past = L1.bla_past
	sum bla_any
	sum bla_any if l1_bla_past==0
	sum bla_any if l1_bla_past==1

*5) Country characteristics by BLA signing in the past 10 years

	*a) country characteristics

	use "$maindir/nat_BLA_data_1945_2020", clear

	tsset code year

	forval i = 1/10 {
	
		gen l`i'_bla_any = L`i'.bla_any
		
	}
	
	egen bla_any_past10 = rowmax(bla_any l?_bla_any l??_bla_any)
	
	gen pc_rgdpe = rgdpe/pop
	gen emp_rate = emp/pop
	gen trade_share = (imports_all+exports_all)/(pl_gdpo*cgdpo*1000000)
	
	*BLA signers
	cap mat drop sum_nat_bla1
	foreach var in pc_rgdpe pop emp_rate avh trade_share polity2 deaths_conflicts {
	
		qui sum `var' if bla_any_past10==1, d
		mat tempsum = [`r(mean)', `r(sd)', `r(p10)', `r(p90)']
		mat rownames tempsum = `var'
		mat sum_nat_bla1 = nullmat(sum_nat_bla1)\tempsum
		
	}
	
	*BLA non-signers
	cap mat drop sum_nat_bla0
	foreach var in pc_rgdpe pop emp_rate avh trade_share polity2 deaths_conflicts {
	
		qui sum `var' if bla_any_past10==0, d
		mat tempsum = [`r(mean)', `r(sd)', `r(p10)', `r(p90)']
		mat sum_nat_bla0 = nullmat(sum_nat_bla0)\tempsum
		
	}
	
	mat sum_nat = sum_nat_bla1,sum_nat_bla0
	
	*Count
	count if bla_any_past10==1
	scalar count_nat_bla1 = `r(N)'
	
	count if bla_any_past10==0
	scalar count_nat_bla0 = `r(N)'
	
	*Effect sizes
	gen bla_any_past10_rev = bla_any_past10 //replacing 0 with 2 so we get the right sign
	replace bla_any_past10_rev = 2 if bla_any_past10==0
	
	cap mat drop esizes_mat
	foreach var in pc_rgdpe pop emp_rate avh trade_share polity2 deaths_conflicts {
	
		esize twosample `var', by(bla_any_past10_rev)
		mat tempsum = [`r(d)', `r(lb_d)', `r(ub_d)']
		mat rownames tempsum = `var'
		mat esizes_mat = nullmat(esizes_mat)\tempsum
		
	}
	
	*b) dyad characteristics

	use "$maindir/dyad_BLA_data_1945_2020", clear

	tsset dyad_id year

	forval i = 1/10 {
	
		gen l`i'_bla_any = L`i'.bla_any
		
	}
	
	egen bla_any_past10 = rowmax(bla_any l?_bla_any l??_bla_any)
	
	gen pc_rgdpeA = rgdpeA/popA
	gen pc_rgdpeB = rgdpeB/popB
	gen pc_rgdpe_diff = abs(pc_rgdpeA-pc_rgdpeB)
	
	gen emp_rateA = empA/popA
	gen emp_rateB = empB/popB
	gen emp_rate_diff = abs(emp_rateA-emp_rateB)
	
	gen polity2_diff = abs(polity2A-polity2B)
	
	*Identifying "host"
	sort dyad_id year
	by dyad_id: gen gdpnonmiss = _n if !missing(pc_rgdpe_diff)
	by dyad_id: egen mingdpnonmiss = min(gdpnonmiss)
	
	gen first_host = codeA if pc_rgdpeA>pc_rgdpeB & !missing(pc_rgdpe_diff) & gdpnonmiss==mingdpnonmiss
	replace first_host = codeB if pc_rgdpeA<pc_rgdpeB & !missing(pc_rgdpe_diff) & gdpnonmiss==mingdpnonmiss
	
	by dyad_id: egen code_host = max(first_host)
	kountry code_host, from(cown) geo(men)
	gen MENA_host = GEO=="Middle East"
	drop GEO NAMES
	
	*GDP difference for MENA vs. non-MENA hosts
	gen pc_rgdpe_diff_MENA = pc_rgdpe_diff if MENA_host==1
	gen pc_rgdpe_diff_notMENA = pc_rgdpe_diff if MENA_host==0
	
	*GDP per capita ratio host to source
	gen pcgdp_ratio_host_source = ln(pc_rgdpeA)/ln(pc_rgdpeB) if codeA==code_host
	replace pcgdp_ratio_host_source = ln(pc_rgdpeB)/ln(pc_rgdpeA) if codeB==code_host
	
	*Higher Polity2 score in source
	gen higher_polity2_source = 0 if !missing(polity2A) & !missing(polity2B)
	replace higher_polity2_source = 1 if ((polity2A>polity2B & codeB==code_host) | (polity2A<polity2B & codeA==code_host)) & !missing(higher_polity2_source)
	
	*Treaty onset
	sort dyad_id year
	by dyad_id: gen onset = bla_past - bla_past[_n-1]
	replace onset = 0 if onset==.
	gen after_onset = onset==0 & bla_past==1
	
	*Time
	gen time = year-1945
	gen time_2 = time^2
	gen time_3 = time^3
	
	*Regressions to replicate Table 3 in Chilton/Posner
	#delimit ;
	
		local controls `"
		"c.pcgdp_ratio_host_source"
		"1.higher_polity2_source"
		"c.pcgdp_ratio_host_source 1.higher_polity2_source"
		"c.pcgdp_ratio_host_source 1.MENA_host 1.MENA_host#c.pcgdp_ratio_host_source"
		"1.higher_polity2_source 1.MENA_host 1.higher_polity2_source#1.MENA_host"
		"c.pcgdp_ratio_host_source 1.MENA_host 1.MENA_host#c.pcgdp_ratio_host_source
			1.higher_polity2_source 1.higher_polity2_source#1.MENA_host"
		"';
	
	#delimit cr
	
	local i = 1
	
	foreach c in `controls' {
	 
		di "`c'"
		qui logit onset `c' time time_2 time_3 if after_onset==0, vce(cl dyad_id)
		
		est store est`i++'
		
		
	}
	
	#delimit ;

	esttab est1 est2 est3 est4 est5 est6 using "$maindir\graphs and maps\reg_chilton_posner", 
		keep(pcgdp_ratio_host_source 1.higher_polity2_source 1.MENA_host#c.pcgdp_ratio_host_source
		1.higher_polity2_source#1.MENA_host 1.MENA_host) 
		order(pcgdp_ratio_host_source 1.higher_polity2_source 1.MENA_host#c.pcgdp_ratio_host_source
		1.higher_polity2_source#1.MENA_host 1.MENA_host)
		coeflab(pcgdp_ratio_host_source "GDP per Capita Raio (host/source)" 1.higher_polity2_source
		"Higher Source Polity Score" 1.MENA_host#c.pcgdp_ratio_host_source "GDP per Capita Ratio \(\times\) Middle Eastern Host"
		1.higher_polity2_source#1.MENA_host "Higher Source Polity Score \(\times\) Middle Eastern Host" 1.MENA_host "Middle Eastern Host")
		b(3) se(3) booktabs label nolegend nonotes replace star(* 0.1 ** 0.05 *** 0.01)
		stats(r2_p N, labels("\(Pseudo-R^2\)" "\(N\)") fmt(3 0)) f nomtitles nonumbers 
		prehead("\begin{table}[!htbp]
		\fontsize{9}{10}\selectfont
		\caption{Treaty onset}
		\begin{tabularx}{\textwidth}{p{6cm}XXXXXX}
		\toprule
		& (1) & (2) & (3) & (4) & (5) & (6) \\")
		postfoot("\bottomrule 
		\addlinespace
		\multicolumn{7}{p{\textwidth}}{\fontsize{9}{10}\selectfont \textbf{Note}:}
		\end{tabularx}
		\end{table}");
			
	#delimit cr	
	
	
	*BLA signers
	cap mat drop sum_dyad_bla1
	foreach var in pc_rgdpe_diff pc_rgdpe_diff_MENA pc_rgdpe_diff_notMENA emp_rate_diff polity2_diff dist same_region{
	
		qui sum `var' if bla_any_past10==1, d
		mat tempsum = [`r(mean)', `r(sd)', `r(p10)', `r(p90)']
		mat rownames tempsum = `var'
		mat sum_dyad_bla1 = nullmat(sum_dyad_bla1)\tempsum
		
	}
	
	*BLA non-signers
	cap mat drop sum_dyad_bla0
	foreach var in pc_rgdpe_diff pc_rgdpe_diff_MENA pc_rgdpe_diff_notMENA emp_rate_diff polity2_diff dist same_region{
	
		qui sum `var' if bla_any_past10==0, d
		mat tempsum = [`r(mean)', `r(sd)', `r(p10)', `r(p90)']
		mat sum_dyad_bla0 = nullmat(sum_dyad_bla0)\tempsum
		
	}
	
	mat sum_dyad = sum_dyad_bla1,sum_dyad_bla0
	
	*Count
	count if bla_any_past10==1
	scalar count_dyad_bla1 = `r(N)'
	
	count if bla_any_past10==0
	scalar count_dyad_bla0 = `r(N)'
	
	*Effect sizes
	gen bla_any_past10_rev = bla_any_past10 //replacing 0 with 2 so we get the right sign
	replace bla_any_past10_rev = 2 if bla_any_past10==0
	
	foreach var in pc_rgdpe_diff pc_rgdpe_diff_MENA pc_rgdpe_diff_notMENA emp_rate_diff polity2_diff dist same_region {
	
		esize twosample `var', by(bla_any_past10_rev)
		mat tempsum = [`r(d)', `r(lb_d)', `r(ub_d)']
		mat rownames tempsum = `var'
		mat esizes_mat = nullmat(esizes_mat)\tempsum
		
	}
	
	*TeX tables
	
	local count_nat_bla1 = count_nat_bla1
	local count_nat_bla0 = count_nat_bla0
	local count_dyad_bla1 = count_dyad_bla1
	local count_dyad_bla0 = count_dyad_bla0

	#delimit;	
		
		esttab mat(sum_nat,fmt(%9.2g)) using "$maindir\graphs and maps\sumstats_main" 
			, replace booktabs gaps f coeflab(pc_rgdpe "Real GDP per capita" pop "Population (millions)" emp_rate 
			"Employment to population ratio" avh "Annual hours worked" trade_share "Trade to GDP ratio" polity2
			"Polity2 score" deaths_conflicts "Deaths from armed conflicts") 
			nomtitles collabels("Mean" "SD" "\thead{10th \\pct}" "\thead{90th \\pct}" "Mean" "SD" "\thead{10th \\pct}" "\thead{90th \\pct}")
			substitute(".&" "&" ".\\" "\\" "  ." "  0.") 
			prehead("\begin{table}[!htbp]
			\fontsize{9}{10}\selectfont
			\caption{Country and dyad characteristics.}
			\label{t:sumstats}
			\begin{tabularx}{\textwidth}{p{7cm}XXXXXXXX}
			\toprule
			& \multicolumn{4}{l}{\textbf{\thead{Signed a BLA\\in the past 10 yrs.}}} 
			& \multicolumn{4}{l}{\textbf{\thead{Did not sign a BLA\\in the past 10 yrs.}}}\\
			 & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\
			\addlinespace")
			posthead("\midrule \vspace{12mm}\\
			\multicolumn{9}{l}{\emph{Panel A: Country-level}}\\
			\addlinespace
			\addlinespace")
			prefoot("\midrule
			\addlinespace
			N & \multicolumn{4}{l}{`count_nat_bla1'} & \multicolumn{4}{l}{`count_nat_bla0'} \\");

		esttab mat(sum_dyad,fmt(%9.2g)) using "$maindir\graphs and maps\sumstats_main" 
			, append booktabs gaps f coeflab(pc_rgdpe_diff "Difference in GDP per capita" 
			pc_rgdpe_diff_MENA "Difference in GDP per capita (ME host)"  pc_rgdpe_diff_notMENA "Difference in GDP per capita (non-ME host)"
			emp_rate_diff "Difference in emp/pop ratios" polity2_diff "Difference in Polity2 scores" dist 
			"Distance between population centers" same_region "Same region") 
			nomtitles collabels(none) substitute(".&" "&" ".\\" "\\" "  ." "  0.")
			posthead("\midrule \vspace{12mm}\\
			\multicolumn{9}{l}{\emph{Panel B: Dyad-level}}\\
			\addlinespace
			\addlinespace")
			postfoot("\midrule
			\addlinespace
			N & \multicolumn{4}{l}{`count_dyad_bla1'} & \multicolumn{4}{l}{`count_dyad_bla0'} \\
			\bottomrule 
			\addlinespace
			\multicolumn{9}{p{\textwidth}}{\fontsize{9}{10}\selectfont Panel A presents country-level
			characteristics and Panel B dyad-level characteristics. Columns (1)-(4) summarize the listed
			variables for countries or dyads which signed at least 1 BLA within the past 10 years and 
			columns (5)-(8) for those without a BLA in the past 10 years.}
			\end{tabularx}
			\end{table}");
				
	#delimit cr	
	
	*Graph of effect sizes
	svmat2 esizes_mat, names(esize_est esize_lb esize_ub) rnames(esizevar) 
	gen yvarnum = 8-_n in 1/7
	replace yvarnum = 15 - _n in 8/14
	
	gen esize_type = "nat" in 1/7
	replace esize_type = "dyad" in 8/14
	
	*a) National
	
	#delimit ;
	
	twoway (rcap esize_lb esize_ub yvarnum if esize_type=="nat", horizontal lc(gs11))
		(scatter yvarnum esize_est if esize_type=="nat", mc(black) msize(*0.7)), xlab(-0.75(0.25)0.75)
		xline(0, lw(*0.5) lc(gs5) lp(dash)) ylab(1 "Deaths from armed conflicts" 
		2 "Polity2 score" 3 "Trade to GDP ratio" 4 "Annual hours worked" 5 "Employment to population ratio" 
		6 "Population" 7 "Real GDP per capita", labsize(*0.6) angle(0)) yt("")
		title("A: Country-level") graphregion(color(white)) name(nat_esizes, replace)
		legend(off);
		
	twoway (rcap esize_lb esize_ub yvarnum if esize_type=="dyad", horizontal lc(gs11))
		(scatter yvarnum esize_est if esize_type=="dyad", mc(black) msize(*0.7)), xlab(-0.75(0.25)0.75)
		xline(0, lw(*0.5) lc(gs5) lp(dash)) ylab(1 "Same region" 
		2 "Distance between pop. centers" 3 "Difference in Polity2 scores" 
		4 "Difference in emp/pop ratios" 5 "Difference in GDP per capita (non-ME host)" 
		6 "Difference in GDP per capita (ME host)" 7 "Difference in GDP per capita", 
		labsize(*0.6) angle(0)) yt("") title("B: Dyad-level") graphregion(color(white)) 
		name(dyad_esizes, replace) legend(off);
		
	#delimit cr
	
	graph combine nat_esizes dyad_esizes, graphregion(color(white)) rows(2) ysize(6)
	graph export "$maindir/graphs and maps/esizes.pdf", replace
	graph export "$maindir/graphs and maps/esizes.emf", replace
	
********************************************************************************
****************************** 4 - Dyad-level **********************************
********************************************************************************

	use "$maindir/dyad_BLA_data_1945_2020", clear

	tsset dyad_id year

	gen l1_bla_past = L1.bla_past
	sum bla_any
	sum bla_any if l1_bla_past==0
	sum bla_any if l1_bla_past==1
